const express = require('express')
const router = express.Router()
const foodModel = require('../db/model/foodModel')

//数据添加
/**
 * @apiGroup Food
 * @api {post} /food/add 菜品添加
 * @apiName add
 * @apiParam {String} name 菜名
 * @apiParam {String} price 价格
 * @apiParam {String} desc 描述
 * @apiParam {String} typeName 类别
 * @apiParam {Number} typeId 类别代码:   1-热菜 2-凉菜 3-酒水
 * @apiParam {String} img 图片
 */
router.post('/add', (req, res)=>{
    //add测试
    // let data = {
    //     name : '麻婆豆腐',
    //     price : '20',
    //     desc : '热卖推荐',
    //     typeName : '热菜',
    //     typeId : 1,
    //     img : '/public/test.jpg'
    // }

    let {name, price, desc, typeName, typeId, img} = req.body
    //参数判断
    foodModel.insertMany({name, price, desc, typeName, typeId, img})
    .then((data)=>{
        res.send({err:0, msg:'添加成功'})
    })
    .catch((err)=>{
        res.send({err:-1, msg:'添加失败'})
    })
})

//类别代码查询
/**
 * @apiGroup Food
 * @api {post} /food/getInfoByType 类别代码查询
 * @apiName getInfoByType
 * @apiParam {Number} typeId 类别代码
 */
router.post('/getInfoByType', (req, res)=>{
    let {typeId} = req.body
    foodModel.find({typeId})
    .then((data)=>{
        res.send({err:0, msg:'查询成功', list:data})
    })
    .catch((err)=>{
        res.send({err:-1, msg:'查询失败'})
    })
})

//分类查询
/**
 * @apiGroup Food
 * @api {post} /food/getInfoByKeyword 分类查询
 * @apiName getInfoByKeyword
 * @apiParam {String} keyWord 关键字
 */
router.post('/getInfoByKeyword', (req, res)=>{
    let {keyWord} = req.body
    //正则式匹配关键字
    let reg = new RegExp(keyWord) 
    //name或desc查询
    foodModel.find({$or:[{name:{$regex:reg}}, {desc:{$regex:reg}}]}) 
    .then((data)=>{
        res.send({err:0, msg:'查询成功', list:data})
    })
    .catch((err)=>{
        res.send({err:-1, msg:'查询失败'})
    })
})

//删除(ObjectId)
/**
 * @apiGroup Food
 * @api {post} /food/del 菜品删除
 * @apiName del
 * @apiParam {String} _id ObjectId
 */
router.post('/del', (req, res)=>{
    let {_id} = req.body
    foodModel.deleteMany({_id})
    .then((data)=>{
        res.send({err:0, msg:'删除成功'})
    })
    .catch((err)=>{
        res.send({err:-1, msg:'删除失败'})
    })
})

//修改更新(Json)
/**
 * @apiGroup Food
 * @api {post} /food/update 修改更新
 * @apiName update
 * @apiParam {String} _id ObjectId
 * @apiParam {String} name 菜名
 * @apiParam {String} price 价格
 * @apiParam {String} desc 描述
 * @apiParam {String} typeName 类别
 * @apiParam {Number} typeId 类别代码
 * @apiParam {String} img 图片
 */
router.post('/update', (req, res)=>{
    let {name, price, desc, typeName, typeId, img, _id} = req.body
    foodModel.updateOne({_id},{name, price, desc, typeName, typeId, img})
    .then((data)=>{
        res.send({err:0, msg:'修改成功'})
    })
    .catch((err)=>{
        res.send({err:-1, msg:'修改失败'})
    })
})

//分页查询
/**
 * @apiGroup Food
 * @api {post} /food/getInfoByPage 分页查询
 * @apiName getInfoByPage
 * @apiParam {Number} pageSize 每页数据条数
 * @apiParam {Number} page 哪一页
 */
router.post('/getInfoByPage', (req, res)=>{
    let pageSize = req.body.pageSize || 5   //用户不传参时，默认值为5
    let page = req.body.page || 1
    let count = 0
    foodModel.find()
    .then((list)=>{
        count = list.length //获取总数据条数
        return foodModel.find().limit(Number(pageSize)).skip(Number((page - 1) * pageSize))
    })
    .then((data)=>{
        // res.send({err:0, msg:'查询成功',list:data})
        let allpage = Math.ceil(count / pageSize)
        res.send({err:0, msg:'查询成功',info:{list:data, count:count, allpage:allpage}})
    })
    .catch((err)=>{
        res.send({err:-1, msg:'查询失败'})
    })
})



module.exports = router